package cn.magictodo.weblog.admin.service.impl;

import cn.magictodo.weblog.admin.model.vo.user.FindUserInfoRspVO;
import cn.magictodo.weblog.admin.model.vo.user.UpdateAdminUserPasswordReqVO;
import cn.magictodo.weblog.admin.service.AdminUserService;
import cn.magictodo.weblog.common.domain.dos.UserDO;
import cn.magictodo.weblog.common.domain.mapper.UserMapper;
import cn.magictodo.weblog.common.enums.ResponseCodeEnum;
import cn.magictodo.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import static cn.magictodo.weblog.common.enums.RedisPrefixEnum.REDIS_USER_TOKEN;

/**
 * 用户相关代码
 *
 * @author liu
 * #time 2023-11-21 14:37
 */
@Service
@Slf4j
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private UserMapper userMapper;
    /**
     * 修改密码
     * @param updateAdminUserPasswordReqVO
     * @return 修改结果
     */
    @Override
    public Response updatePassword(UpdateAdminUserPasswordReqVO updateAdminUserPasswordReqVO) {
        // 拿到用户名、密码
        String username = updateAdminUserPasswordReqVO.getUsername();
        String orderPassword = updateAdminUserPasswordReqVO.getOrderPassword();
        String password = updateAdminUserPasswordReqVO.getNewPassword();

        UserDO userDO = userMapper.findByUserName(username);

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //验证密码是否正确
        boolean matches = passwordEncoder.matches(orderPassword, userDO.getPassword());
        if(matches){
        String encodePassword = passwordEncoder.encode(password);
        // 更新到数据库
        int count = userMapper.updatePasswordByUsername(username, encodePassword);
        return count == 1 ? Response.success() : Response.fail(ResponseCodeEnum.USERNAME_OR_PWD_ERROR);
        }else{
            return Response.fail(ResponseCodeEnum.USERNAME_OR_PWD_ERROR);
        }
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    @Override
    public Response findUserInfo() {
        // 获取存储在 ThreadLocal 中的用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 拿到用户名
        String username = authentication.getName();

        return Response.success(FindUserInfoRspVO.builder().username(username).build());
    }

    /**
     * 退出登录
     * @return
     */
    @Override
    public Response logout() {
        // 获取存储在 ThreadLocal 中的用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 拿到用户名
        String username = authentication.getName();
        try{
            redisTemplate.delete(REDIS_USER_TOKEN.getPrefix()+username);
            return Response.success();
        }catch (Exception e){
            e.printStackTrace();
            return Response.fail();
        }
    }
}
